package com.tanhua.dubbo.server.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Users;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service(version = "1.0.0")
public class UserApiImpl implements UserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 将好友信息保存至数据库中
     *
     * @param id
     * @param friendId
     * @return
     */
    @Override
    public String saveFriend(Long id, Long friendId) {
        if (!ObjectUtil.isAllNotEmpty(id, friendId)) {
            return null;
        }
        //判断两者的好友关系是否存在
        Query query = Query.query(Criteria.where("userId").is(id).and("friendId").is(friendId));
        long count = this.mongoTemplate.count(query, Users.class);
        if (count > 0) {
            return null;
        }
        Users users = new Users();
        users.setId(ObjectId.get());
        users.setUserId(id);
        users.setFriendId(friendId);
        users.setDate(System.currentTimeMillis());
        //注册我与好友的关系
        mongoTemplate.save(users);
        users.setId(ObjectId.get());
        users.setUserId(friendId);
        users.setFriendId(id);
        //注册好友与我的关系
        mongoTemplate.save(users);

        return users.getId().toHexString();
    }

    @Override
    public boolean removeFriend(Long userId, Long friendId) {
        if (!ObjectUtil.isAllNotEmpty(userId, friendId)) {
            return false;
        }
        //判断两者的好友关系是否存在
        Query query = Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        List<Users> list = mongoTemplate.find(query, Users.class);
        //如果是空说明好友关系不存在
        if (CollUtil.isEmpty(list)) {
            return false;
        }
        Query query1 = Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        long count1 = mongoTemplate.remove(query1, Users.class).getDeletedCount();

        Query query2 = Query.query(Criteria.where("userId").is(friendId).and("friendId").is(userId));
        long count2 = mongoTemplate.remove(query2, Users.class).getDeletedCount();

        return count1 > 0 && count2 > 0;
    }

    /**
     *
     * @param id
     * @param page
     * @param pageSize
     * @param keyWord
     * @return
     */
    @Override
    public PageInfo<Users> findContactsList(Long id, Integer page, Integer pageSize, String keyWord) {
        PageRequest pageRequest = PageRequest.of(page-1,pageSize, Sort.by(Sort.Order.desc("date")));
        Query query = Query.query(Criteria.where("userId").is(id)).with(pageRequest);
        List<Users> users = mongoTemplate.find(query, Users.class);

        PageInfo<Users> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRecords(users);
        return pageInfo;
    }

    /**
     * 查询好友列表不带分页
     *
     * @param id
     * @return
     */
    @Override
    public List<Users> findContactsList(Long id) {
        Query query = Query.query(Criteria.where("userId").is(id));
        List<Users> users = mongoTemplate.find(query, Users.class);
        return users;
    }
}
